package com.iyao_app.adapter;

import android.net.Uri;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyaowoqu.base_library.util.ImageUtils;
import com.iyaowoqu.base_library.util.LogUtil;

import java.util.List;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/12/6.
 */

public class PublishAdapter extends BaseItemDraggableAdapter<String, BaseViewHolder> {
    int mType = 0;

    public PublishAdapter(List<String> data) {
        super(R.layout.item_publish_, data);
    }

    public void updateType(int type) {
        if (type != mType) {
            mType = type;
            notifyDataSetChanged();
        }
    }

    @Override
    protected void convert(final BaseViewHolder helper, String item) {
        final int position = helper.getLayoutPosition();
        ImageView mIvDelete = helper.getView(R.id.iv_delete);
        SimpleDraweeView mSdvPhoto = helper.getView(R.id.sdv_photo);
//        if (mType == 0) {
//            mIvDelete.setVisibility(View.GONE);
//        } else {
//            mIvDelete.setVisibility(View.VISIBLE);
//        }
        if ("mipmap".equals(item)) {
            mIvDelete.setVisibility(View.GONE);

            mSdvPhoto.setImageResource(R.mipmap.btn_tianjia);
        } else {
            mIvDelete.setVisibility(View.GONE);

            mIvDelete.setVisibility(View.VISIBLE);
            try {
                String url = null;
                if (item.startsWith("http")) {
                    url = item;
                } else {
                    url = ImageUtils.IMAGE_FILE + item;
                }
                Uri uri = Uri.parse(url);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(200, 200))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(mSdvPhoto.getController())
                        .setImageRequest(request)
                        .build();
                mSdvPhoto.setController(controller);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mIvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.e("===删除==");
                    remove(position);
                    if (getData().size() < 9) {
                        if (!getData().contains("mipmap")) {
                            addData("mipmap");
                        }
                    }
                }
            });
        }

    }
}
